package com.clouds.wms.pur;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.clouds.wms.bean.Cmsmd;
import com.clouds.wms.bean.Cmsmk;
import com.clouds.wms.bean.Purma;
import com.clouds.wms.exception.SysException;
import com.clouds.wms.service.CmsmjService;
import com.clouds.wms.service.CmsmkService;
import com.clouds.wms.service.PurmaService;
import com.clouds.wms.service.PurmbService;
import com.clouds.wms.util.Function;
import com.clouds.wms.util.PagesBean;
import com.clouds.wms.util.RequestHelper;
import com.clouds.wms.util.SysParams;

@Controller
@RequestMapping("/purma")
public class PurmaController {
	private Log log = LogFactory.getLog(getClass());
	@Resource(name="purmaService")
	private PurmaService purmaService;
	@Resource(name="purmbService")
	private PurmbService purmbService;
	@Resource(name="cmsmjService")
	private CmsmjService cmsmjService;
	@Resource(name="cmsmkService")
	private CmsmkService cmsmkService;
	@RequestMapping(value = "/purmaMain.xhtml")
	public String purmaMain(HttpServletRequest request, HttpServletResponse response, Model model) throws SysException {
		model.addAttribute("allCmsmjList", cmsmjService.getCmsmjListByMjac(SysParams.DOCUMENT_CATEGORY_CGDD));
		return "pur/purma";
	}
	@RequestMapping(value = "/purmaList.xhtml")
	public String purmaList(HttpServletRequest request, HttpServletResponse response, Model model) throws SysException {
		String maaa = RequestHelper.getString(request, "maaa");
		String maab = RequestHelper.getString(request, "maab");
		String maac = RequestHelper.getString(request, "maac");
		String startMaad = RequestHelper.getString(request, "startMaad");
		String endMaad = RequestHelper.getString(request, "endMaad");
		int pageNumber = RequestHelper.getInt(request, "page", 1);// 要跳转的页号
		int pageSize = RequestHelper.getInt(request, "rows", 10);// 一页的条数
		PagesBean pb = purmaService.getPurmaList(maaa, maab, maac, startMaad, endMaad, pageNumber, pageSize);
		List<Purma> list = (List<Purma>) pb.getData();
		int count = pb.getCount();
		Map<String,Object> paraMap = new HashMap<String,Object>();
		paraMap.put("total", count);
		paraMap.put("rows", list);
		try {
			Function.toJson(response, paraMap);
		} catch (IOException e) {
			log.error(e.getMessage());
		}
		return null;
	}
	/**
	 * 添加采购单单头
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws SysException
	 * @throws IOException 
	 */
	@RequestMapping(value = "/addPurma.xhtml", method = RequestMethod.POST)
	public String addPurma(HttpServletRequest request, HttpServletResponse response, Model model, Purma purma) throws SysException, IOException {
		String maaa = purma.getMaaa();
		String maab = purma.getMaab();
		Cmsmd globalUmUser = (Cmsmd)request.getSession().getAttribute("globalUmUser");
		response.setContentType("text/xml;charset=utf-8");
		response.setHeader("Pragma", "No-cache");
		response.setDateHeader("Expires", 0);
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		Purma purmatmp = purmaService.getPurma(maaa,maab);
		if(purmatmp != null && purmatmp.getMaaa() != null && !"".equals(purmatmp.getMaaa())) {
			out.print("该采购订单的编号已经存在，请重新录入!");
			return null;
		}
		purma.setMaad(Function.getDateString(new Date()));
		purmaService.savePurca(purma);
		Cmsmk cmsmk = new Cmsmk();
		cmsmk.setMkab(String.valueOf(globalUmUser.getMdaa()));
		cmsmk.setMkac(globalUmUser.getMdac());
		cmsmk.setMkad("增加了编号为[" + maab + "]的采购订单!");
		cmsmk.setMkae(new Date());
		cmsmk.setMkaf(request.getRemoteAddr());
		cmsmkService.saveCmsmk(cmsmk);
		out.print("success");
		return null;
	}
	
}
